// pages/addLesson/addLesson.js
var common = require("../../utils/util.js");
var tcity = require("../../utils/citys.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
      provinces: [],
      province: "",
      citys: [],
      city: "",
      countys: [],
      county: '',
      value: [0, 0, 0],
      values: [0, 0, 0],
      condition: false,
    lessonName:'',
      type:'',
      register: [
          {
              title: '基本信息',
              num: 1
          },
          {
              title: '调查问题',
              num: 2
          },
          {
              title: '登记成功',
              num: 3
          }
      ],
      currindex: 0,
      gender: ['男','女'],
      gender_index: 0,
      time:'获取',
      info:{
          PsnName: '',
          PsnMobile: '',
          PsnEmail: '',
          PsnComName: '',
          PsnAddress: '',
          PsnGender: '',
          ExhID: '',
          PsnProvince: '',
          PsnCity: '',
          PsnCountry: '',
          OpenID: '',
          PhotoUrl: '',
          CardUrl: '',
          FaceID: '',
          code:'',
          IfSubmit: ''
      }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  bindChange: function(e) {
      //console.log(e);
      var val = e.detail.value
      var t = this.data.values;
      var cityData = this.data.cityData;

      if(val[0] != t[0]){
          console.log('province no ');
          const citys = [];
          const countys = [];

          for (let i = 0 ; i < cityData[val[0]].sub.length; i++) {
              citys.push(cityData[val[0]].sub[i].name)
          }
          for (let i = 0 ; i < cityData[val[0]].sub[0].sub.length; i++) {
              countys.push(cityData[val[0]].sub[0].sub[i].name)
          }

          this.setData({
              province: this.data.provinces[val[0]],
              city: cityData[val[0]].sub[0].name,
              citys:citys,
              county: cityData[val[0]].sub[0].sub[0].name,
              countys:countys,
              values: val,
              value:[val[0],0,0]
          })

          return;
      }
      if(val[1] != t[1]){
          console.log('city no');
          const countys = [];

          for (let i = 0 ; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
              countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
          }

          this.setData({
              city: this.data.citys[val[1]],
              county: cityData[val[0]].sub[val[1]].sub[0].name,
              countys:countys,
              values: val,
              value:[val[0],val[1],0]
          })
          return;
      }
      if(val[2] != t[2]){
          console.log('county no');
          this.setData({
              county: this.data.countys[val[2]],
              values: val
          })
          return;
      }


  },
  open:function(){
        this.setData({
            condition:!this.data.condition
        })
    },
  send_code: function() {
      if (this.data.time > 0) {
          return false
      }
      console.log(this.data.info.PsnMobile)
      if (!common.is_mobile(this.data.info.PsnMobile)) {
          wx.showToast({
              title: '请输入正确的手机号',
              icon: 'none'
          })
      } else {
          var that = this
          common.bapiajax({
              url: 'GetVerificationCode',
              data: {
                  mobile: this.data.info.PsnMobile
              },
              loading: '发送中...',
              success: function (res) {
                  if (res.status == 'SUCCESS') {
                      that.setData({
                          time: 60
                      })
                      var timer = setInterval(function () {
                          that.data.time--
                          if (that.data.time == 0) {
                              clearInterval(timer)
                          }
                          that.setData({
                              time: that.data.time
                          })
                      }, 1000)
                  } else {
                      common.info(res.result.msg)
                  }
              },
              fail: function () {
                  common.info('发送失败，请重试')
              }
          })
      }
  },
  bindeaddress:function (e) {

  },
  bindphone:function (e) {
      var phone = 'info.PsnMobile'
        this.setData({
            [phone]:e.detail.value
        })
      console.log(this.data.info.PsnMobile)
  },
  bindPickerChange:function (e) {
      var gender = 'info.PsnGender'
      var that = this;
      console.log('picker发送选择改变，携带值为', e.detail.value)
      that.setData({
          gender_index: e.detail.value
      })
      var calue = that.data.gender[e.detail.value]
      that.setData({
        [gender]: calue
      })
  },
  addregist:function (e) {
      this.setData({
          currindex: e.currentTarget.dataset.index
      })
      console.log(this.data.currindex)
  },
  onLoad: function (options) {
      wx.setNavigationBarTitle({
          title: '基本信息'
      });
      var that = this;
      console.log(options.id)
      that.setData({
          type: options.id
      })
      console.log("onLoad");
      var that = this;

      tcity.init(that);

      var cityData = that.data.cityData;


      const provinces = [];
      const citys = [];
      const countys = [];

      for(let i=0;i<cityData.length;i++){
          provinces.push(cityData[i].name);
      }
      console.log('省份完成');
      for (let i = 0 ; i < cityData[0].sub.length; i++) {
          citys.push(cityData[0].sub[i].name)
      }
      console.log('city完成');
      for (let i = 0 ; i < cityData[0].sub[0].sub.length; i++) {
          countys.push(cityData[0].sub[0].sub[i].name)
      }

      that.setData({
          'provinces': provinces,
          'citys':citys,
          'countys':countys,
          'province':cityData[0].name,
          'city':cityData[0].sub[0].name,
          'county':cityData[0].sub[0].sub[0].name
      })
      console.log('初始化完成');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})